package top.juc.plugin.idea.openfeignx.listener;

import com.intellij.openapi.startup.StartupManager;
import kotlin.collections.CollectionsKt;
import top.juc.plugin.idea.openfeignx.annotation.Annotation;
import top.juc.plugin.idea.openfeignx.util.CacheUtils;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManagerListener;
import org.jetbrains.annotations.NotNull;

public class ProjectOpenCloseListener  implements ProjectManagerListener {

    /**
     * Invoked on project open.
     *
     * @param project opening project
     */
    @Override
    public void projectOpened(@NotNull Project project) {
        // Ensure this isn't part of testing
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            return;
        }
        StartupManager.getInstance(project).runWhenProjectIsInitialized(() -> {
            CacheUtils.init(project, CollectionsKt.listOf(Annotation.FEIGN_CLIENT, Annotation.REST_CONTROLLER));
        });

//        ApplicationManager.getApplication().invokeLater(() -> {
//
//        });
    }


    /**
     * Invoked on project close.
     *
     * @param project closing project
     */
    @Override
    public void projectClosed(@NotNull Project project) {
        // Ensure this isn't part of testing
        if (ApplicationManager.getApplication().isUnitTestMode()) {
            return;
        }
        CacheUtils.clearProject(project, Annotation.FEIGN_CLIENT);
        CacheUtils.clearProject(project, Annotation.REST_CONTROLLER);
    }

}
